package com.cz.video.framework.provider;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import com.cz.video.common.constant.CommonConstants;
import com.cz.video.common.core.domain.bo.SysSensitiveWordBo;
import com.cz.video.common.core.domain.vo.SysSensitiveWordVo;
import com.cz.video.common.core.service.SysSensitiveWordService;
import com.github.houbb.sensitive.word.api.IWordDeny;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author binbin.hou
 * @since 1.0.0
 */
@Component
@RequiredArgsConstructor
public class WordDenyProvider implements IWordDeny {

    private final SysSensitiveWordService sensitiveWordService;

    /**
     * 允许的敏感词
     * @return 允许的敏感词集合
     */
    @Override
    public List<String> deny() {
        SysSensitiveWordBo wordBo = new SysSensitiveWordBo();
        wordBo.setType(CommonConstants.BANNED_TYPE).setStatus(CommonConstants.NORMAL_STATUS);
        //从数据库查询
        List<SysSensitiveWordVo> wordVos = sensitiveWordService.list(wordBo);
        List<String> denyWords = CollectionUtil.isEmpty(wordVos)? Collections.emptyList(): wordVos.stream()
            .map(SysSensitiveWordVo::getWord).collect(Collectors.toList());
        //从资源目录查询
//        String[] denyWordArr = ResourceUtil.readUtf8Str("sensitive_words.txt").split("、");
//        CollUtil.addAll(denyWords, denyWordArr);
        return denyWords;
    }
}
